/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
 * pack_msg: put msg_t to byte stream
 * unpack_msg: get msg_t from byte stream
 */
#ifndef MSG_STRUCTURE_H_
#define MSG_STRUCTURE_H_
#include <stdlib.h>
#include <stdint.h>

#define MSG_LENGTH_SIZE     (4)
#define OUT_BUF_LENGTH_SIZE (4)
#define FUNCTION_ID_SIZE    (4)
#define MSG_HEADER_SIZE     (MSG_LENGTH_SIZE + OUT_BUF_LENGTH_SIZE + FUNCTION_ID_SIZE)

#define MSG_BUF_SIZE(data_size) (MSG_HEADER_SIZE + (data_size))

#define ERR_MSG_OK              (0)
#define ERR_MSG_GENERIC         (-1)
#define ERR_MSG_SIZE            (-2)

typedef struct {
    uint32_t function_id;
    uint8_t *buf;
    size_t   buf_size;
    size_t   out_buf_size;
} msg_t;

int32_t pack_msg(uint8_t* buf, size_t buf_size, const msg_t *msg);
int32_t unpack_msg(msg_t *msg, const uint8_t *buf, size_t buf_size);

#endif